/* 
 * xml请求支持中间件,依赖于xml2js来处理xml格式内容并且将其转换为json
 *
*/

import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response } from 'express';
const xml2js = require('xml2js');
const parser = new xml2js.Parser();

@Injectable()
export class XMLMiddleware implements NestMiddleware {

  // 参数是固定的Request/Response/next
  use(req: Request, res: Response, next: Function) {
    console.log(req.headers)
    //跨域
    res.header("Access-Control-Allow-Origin", req.headers.origin);
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "x-csrf-token,x-requested-with,Origin, X-Requested-With, Content-Type, Accept,Authorization");
    res.header('Access-Control-Allow-Credentials', 'true') // 允许带上 cookie
    //res.header("Access-Control-Request-Headers','*");
   
    // 获取express原生请求对象req,找到其请求头内容，如果包含application/xml，则执行转换
    if (req.headers['content-type'] && req.headers['content-type'].includes('application/xml')) {
      // 监听data方法获取到对应的参数数据
      req.on('data', mreq => {
        // 使用xml2js对xml数据进行转换
        parser.parseString(mreq, function (err, result) {
          // 将转换后的数据放入到请求对象的req中
          // 这里根据需要做一些修改以补充完善内容
          req['body'] = result;
        })
      })
    }
    if(req.method=='OPTIONS'){
      res
      .status(200)
      .json({});
    }else{
      // 调用next方法进入到下一个中间件或者路由
      next();
    }
   
  }
}